package com.crm.system.service.impl.productManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.DictionaryEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.common.Dictionary;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.productManager.CustomerMemberProductCourse;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.orderManager.CustomerProductOrderDetailEditVO;
import com.crm.model.vo.productManager.CustomerMemberProductCourseVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.service.common.DictionaryDbService;
import com.crm.service.productManager.CustomerMemberProductCourseDbService;
import com.crm.service.productManager.CustomerProductDbService;
import com.crm.system.service.productManager.CustomerMemberProductCourseService;
import com.crm.system.service.productManager.CustomerProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.record.PageBreakRecord;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/5/11 16:39
 */
@Service
public class CustomerMemberProductCourseServiceImpl implements CustomerMemberProductCourseService {

    @Autowired
    private CustomerMemberProductCourseDbService memberProductCourseDbService;
    @Autowired
    private CustomerProductDbService productDbService;
    @Autowired
    private DictionaryDbService dictionaryDbService;

    @Override
    public DhPageInfo<CustomerProductVO> selectNotAddCustomerMemberProductCourse(String productName, String memberProductRelationId,Integer pageIndex,Integer pageSize) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<CustomerProductVO> customerProductList = productDbService.selectNotAddCustomerMemberProductCourse(productName,memberProductRelationId);
        PageInfo<CustomerProductVO> pageInfo = new PageInfo<>(customerProductList);
/*           customerProductList.forEach(customerProduct ->{
            //查询现有的课程列表
            List<CustomerMemberProductCourse> customerMemberProductCourseList = memberProductCourseDbService.selectMemberProductCourseByRelationId(memberProductRelationId);

            CustomerProductVO customerProductVO = new CustomerProductVO();
            BeanUtils.copyProperties(customerProduct,customerProductVO);
            Dictionary dictionary = dictionaryDbService.selectDictionaryByOne(DictionaryEnum.EXCHANGE_METHOD.getTypeCode(),customerProduct.getExchangeMethod());
            customerProductVO.setExchangeMethodValue(dictionary.getDictName());
*//*            //不存在现有课程则添加，存在现有课程则把分页的总条数减1
            if(!customerMemberProductCourseList.stream().filter(customerMemberProductCourse ->
                    customerMemberProductCourse.getCustomerProductId().equals(customerProduct.getCustomerProductId())).findAny().isPresent()){*//*
                customerProductVOList.add(customerProductVO);
*//*            }else {
                pageInfo.setTotal(pageInfo.getTotal()-1);
            }*//*
        });*/
        return new DhPageInfo<CustomerProductVO>(pageInfo, customerProductList);
    }

    /***
     * @Description:添加会员产品课程
     * @Param: customerMemberProductCourseList 课程列表
     * @Param: memberProductRelationId 会员产品关系id
     * @Author: luojie
     * @Date: 2021/5/12 9:49
     */
    @Override
    public Boolean addCustomerMemberProductCourse(List<CustomerMemberProductCourse> customerMemberProductCourseList, String memberProductRelationId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        customerMemberProductCourseList.stream().forEach(customerMemberProductCourse -> {
            customerMemberProductCourse.setMemberProductCourseId(CommonUtils.getGUID());
            customerMemberProductCourse.setCreatePeopleId(userAccount.getAccountId());
            customerMemberProductCourse.setCreateTm(new Date());
            customerMemberProductCourse.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            customerMemberProductCourse.setMemberProductRelationId(memberProductRelationId);
            memberProductCourseDbService.insertCustomerMemberProductCourse(customerMemberProductCourse);
        });
        return true;
    }

    /***
     * @Description: 根据服务项配置关系id查询会员产品课程
     * @Param: memberProductRelationId 服务项关系id
     * @Author: luojie
     * @Date: 2021/5/12 9:53
     */
    @Override
    public List<CustomerMemberProductCourseVO> selectMemberProductCourseByRelationId(String memberProductRelationId) {
        List<CustomerMemberProductCourseVO> customerMemberProductCourseVOList = new ArrayList<>();
        List<CustomerMemberProductCourse> customerMemberProductCourseList = memberProductCourseDbService.selectMemberProductCourseByRelationId(memberProductRelationId);
        customerMemberProductCourseList.stream().forEach(customerMemberProductCourse -> {
            CustomerMemberProductCourseVO customerMemberProductCourseVO = new CustomerMemberProductCourseVO();
            BeanUtils.copyProperties(customerMemberProductCourse,customerMemberProductCourseVO);
            //查询产品名称
            CustomerProduct customerProduct = productDbService.selectCustomerProductByProductId(customerMemberProductCourse.getCustomerProductId());
            customerMemberProductCourseVO.setCustomerProductName(customerProduct.getProductName());
            //查询兑换方式名称
            Dictionary dictionary = dictionaryDbService.selectDictionaryByOne(DictionaryEnum.EXCHANGE_METHOD.getTypeCode(),customerMemberProductCourse.getExchangeMethod());
            customerMemberProductCourseVO.setExchangeMethodValue(dictionary.getDictName());
            customerMemberProductCourseVOList.add(customerMemberProductCourseVO);
        });
        return customerMemberProductCourseVOList;
    }

    /**
     * 根据主键删除服务项课程
     * @param memberProductCourseId 课程id
     * @return
     */
    @Override
    public Boolean delMemberProductCourseByCourseId(String memberProductCourseId) {
        CustomerMemberProductCourse customerMemberProductCourse = new CustomerMemberProductCourse();
        customerMemberProductCourse.setMemberProductCourseId(memberProductCourseId);
        customerMemberProductCourse.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        Boolean result = memberProductCourseDbService.updateMemberProductCourseByCourseId(customerMemberProductCourse);
        return result;
    }

}
